package com.wl.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Redis的工具类
 *
 * @program: treeHole
 * @author: 吴磊
 * @create: 2021-07-06 20:07
 */
public class RedisUtil {
    private static JedisPool JEDIS_POOL;


    public static Jedis getJedis() {
        return JEDIS_POOL.getResource();
    }

    static {
        // 读取配置文件
        InputStream is = RedisUtil.class.getClassLoader().getResourceAsStream("jedis.properties");
        // 创建Properties对象
        Properties pro = new Properties();
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取数据，置到JedisPoolConfig中
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(Integer.parseInt(pro.getProperty("redis.pool.maxIdle")));
        config.setMaxTotal(Integer.parseInt(pro.getProperty("redis.pool.maxTotal")));
        config.setMinIdle(Integer.parseInt(pro.getProperty("redis.pool.minIdle")));
//        config.setMaxWaitMillis(Integer.parseInt(pro.getProperty("redis.pool.maxWaitMillis")));
//        config.setTestOnBorrow(Boolean.parseBoolean(pro.getProperty("redis.pool.testOnBorrow")));
//        config.setTestOnReturn(Boolean.parseBoolean(pro.getProperty("redis.pool.testOnReturn")));
//        config.setTimeBetweenEvictionRunsMillis(Integer.parseInt(pro.getProperty("redis.pool.timeBetweenEvictionRunsMillis")));
//        config.setTestWhileIdle(Boolean.parseBoolean(pro.getProperty("redis.pool.testWhileIdle")));
//        config.setNumTestsPerEvictionRun(Integer.parseInt(pro.getProperty("redis.pool.numTestsPersEvictionRun")));
        String host = pro.getProperty("redis.ip");
        Integer port = Integer.parseInt(pro.getProperty("redis.port"));
        String password = pro.getProperty("redis.password");
        Integer database = Integer.parseInt(pro.getProperty("redis.select"));
        // 初始化JedisPool
        JEDIS_POOL = new JedisPool(config, host, port, 6000, password, database);
    }

    /**
     * Redis的文章精华点赞积分
     *
     * @param key
     * @param id
     */
    public static void setIntegralByPraise(String key, String id, Jedis jedis) {
        if (jedis.zrank(key, id) != null) {
            jedis.zincrby(key, 2, id);
        } else {
            jedis.zadd(key, 2, id);
        }
    }

    /**
     * Redis的文章精华踩积分
     *
     * @param key
     * @param id
     */
    public static void setIntegralByStep(String key, String id, Jedis jedis) {
        if (jedis.zrank(key, id) != null) {
            jedis.zincrby(key, -2, id);
        } else {
            jedis.zadd(key, -2, id);
        }
    }

    /**
     * Redis的文章精华评论积分
     *
     * @param key
     * @param id
     */
    public static void setIntegralByComments(String key, String id, Jedis jedis) {
        if (jedis.zrank(key, id) != null) {
            jedis.zincrby(key, 1, id);
        } else {
            jedis.zadd(key, 1, id);
        }
    }

    /**
     * Redis的文章精华评论积分删除
     *
     * @param key
     * @param id
     */
    public static void setIntegralByDeleteComments(String key, String id, int deleterCount, Jedis jedis) {
        jedis.zincrby(key, -deleterCount, id);
    }
}
